package com.nanfang51g3.ckz.utils;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.os.IBinder;

/**
 * Created by Administrator on 2016/5/10.
 */
public class NetUtils {
    /**
     * 接受网络状态的广播Action
     */
    public static final String NET_BROADCAST_ACTION = "com.network.state.action";

    public static final String NET_STATE_NAME = "network_state";

    /**
     * 实时更新网络状态<br>
     * -1为网络无连接<br>
     * 1为WIFI<br>
     * 2为移动网络<br>
     */
    public static int CURRENT_NETWORK_STATE = -1;

    /**
     * 判断有无网络链接
     * @param mContext
     * @return
     */
    public static boolean checkNetworkInfo(Context mContext) {
        ConnectivityManager conMan = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
                .getState();
        State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                .getState();
        if (mobile == State.CONNECTED || mobile == State.CONNECTING)
            return true;
        if (wifi == State.CONNECTED || wifi == State.CONNECTING)
            return true;
        return false;
    }

    /**
     * 判断网络连接是否打开
     */
    public static boolean CheckNetwork(Context ctx) {
        boolean flag = false;
        ConnectivityManager netManager = (ConnectivityManager) ctx
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (netManager.getActiveNetworkInfo() != null) {
            flag = netManager.getActiveNetworkInfo().isAvailable();
        }
        return flag;
    }

    /**
     * 判断是否是wifi连接
     */
    public static boolean isWifi(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (cm == null)
            return false;
        return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;

    }

    /**
     * 打开网络设置界面
     */
    public static void openSetting(Activity activity) {
        Intent intent = new Intent("/");
        ComponentName cm = new ComponentName("com.android.settings",
                "com.android.settings.WirelessSettings");
        intent.setComponent(cm);
        intent.setAction("android.intent.action.VIEW");
        activity.startActivityForResult(intent, 0);
    }

    /**
     * 开启服务,实时监听网络变化 需要自己在清单文件中配置服务<br>
     * 然后把对应的Action传入<br>
     * 服务类:android.develop.utils.net.NetService
     */
    public static void startNetService(Context context, String action) {
        // 注册广播
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(NET_BROADCAST_ACTION);
        context.registerReceiver(mReceiver, intentFilter);
        // 开启服务
        Intent intent = new Intent();
        KLog.d("开启网络监听服务");
        intent.setAction(action);
        context.bindService(intent, new ServiceConnection() {

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {

            }
        }, Context.BIND_AUTO_CREATE);
    }

    // 接受服务上发过来的广播
    private static BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                CURRENT_NETWORK_STATE = (Integer) intent.getExtras().get(
                        NET_STATE_NAME);
                switch (CURRENT_NETWORK_STATE) {
                    case -1:
                        KLog.d("网络更改为 无网络  CURRENT_NETWORK_STATE ="
                                + CURRENT_NETWORK_STATE);
                        break;
                    case 1:
                        KLog.d("网络更改为 WIFI网络  CURRENT_NETWORK_STATE="
                                + CURRENT_NETWORK_STATE);

                        break;
                    case 2:
                        KLog.d("网络更改为 移动网络  CURRENT_NETWORK_STATE ="
                                + CURRENT_NETWORK_STATE);
                        break;

                    default:
                        break;
                }
            }
        }

    };

}
